package com.sun.androidexerciseproject.widget.activity.ui;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.sun.androidexerciseproject.widget.R;
import com.sun.androidexerciseproject.widget.util.LogUtil;
import com.sun.androidexerciseproject.widget.view.ChoiceView;

import java.util.Arrays;
import java.util.List;

import static android.R.id.list;

public class ListActivity extends Activity {
    private ListView mListView;
    private String[] mStrings = {"Beijing", "shanghai", "tangshan"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        mListView = (ListView) findViewById(list);
        List<String> list = Arrays.asList(mStrings);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, list) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                final ChoiceView view;
                if (null == convertView) {
                    view = new ChoiceView(ListActivity.this);
                } else {
                    view = (ChoiceView) convertView;
                }
                String text = getItem(position);
                view.setText(text);
                return view;
            }
        };
        mListView.setAdapter(adapter);
        if (mListView.getCount() > 0) {
            mListView.setItemChecked(0, true);
        }
        String[] tests = new String[list.size()];
        list.toArray(tests);
        for (String str : tests) {
            LogUtil.log(" str = " + str);
        }
    }
}
